package org.jeecg.modules.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.entity.SysUserPermissionFilter;
import org.jeecg.modules.system.mapper.SysUserPermissionFilterMapper;
import org.jeecg.modules.system.service.ISysPermissionService;
import org.jeecg.modules.system.service.ISysUserPermissionFilterService;
import org.jeecg.modules.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 用户权限过滤表
 * @Author: jeecg-boot
 * @Date: 2023-12-15
 * @Version: V1.0
 */
@Service
public class SysUserPermissionFilterServiceImpl extends ServiceImpl<SysUserPermissionFilterMapper, SysUserPermissionFilter> implements ISysUserPermissionFilterService {

    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private ISysPermissionService sysPermissionService;
    @Autowired
    private IdentityService identityService;

    @Override
    public void setUserFilterPerms(String userId, List<String> permissionIds) {
        Integer tenantId = identityService.getTenantId();
        //1、校验用户存在
        SysUser user = sysUserService.getById(userId);
        if (user == null) throw new RuntimeException("用户不存在！");
        //2、校验权限存在且用户有该权限
        //3、对比旧的过滤权限和新的之间差异，局部增删
        List<SysUserPermissionFilter> filterList = list(new LambdaQueryWrapper<SysUserPermissionFilter>()
                .eq(SysUserPermissionFilter::getTenantId, tenantId)
                .eq(SysUserPermissionFilter::getUserId, userId)
        );
        List<String> oldPermissionIds = filterList.stream().map(SysUserPermissionFilter::getPermissionId).collect(Collectors.toList());
        List<String> finalPermissionIds = new ArrayList<>(permissionIds);
        //最终的减去原来的，等于新加的
        permissionIds.removeAll(oldPermissionIds);
        List<String> adds = new ArrayList<>(permissionIds);
        if (adds.size() != 0) {
            List<SysUserPermissionFilter> filters = sysPermissionService.listByIds(adds).stream().map(sysPermission -> {
                SysUserPermissionFilter permissionFilter = new SysUserPermissionFilter();
                permissionFilter.setTenantId(tenantId);
                permissionFilter.setUserId(userId);
                permissionFilter.setUsername(user.getUsername());
                permissionFilter.setPermissionId(sysPermission.getId());
                permissionFilter.setPermissioinCode(sysPermission.getPerms());
                return permissionFilter;
            }).collect(Collectors.toList());
            saveOrUpdateBatch(filters);
        }
        //原来的减去最终的，等于要删的
        oldPermissionIds.removeAll(finalPermissionIds);
        List<String> dels = new ArrayList<>(oldPermissionIds);
        if (dels.size() != 0) {
            remove(new LambdaQueryWrapper<SysUserPermissionFilter>()
                    .eq(SysUserPermissionFilter::getTenantId, tenantId)
                    .eq(SysUserPermissionFilter::getUserId, userId)
                    .in(SysUserPermissionFilter::getPermissionId, dels));
        }
    }

    @Override
    public List<String> getUserFilterPermsById(String userId) {
        //1、校验用户存在
        SysUser user = sysUserService.getById(userId);
        if (user == null) throw new RuntimeException("用户不存在！");
        return list(new LambdaQueryWrapper<SysUserPermissionFilter>()
                .eq(SysUserPermissionFilter::getTenantId, identityService.getTenantId())
                .eq(SysUserPermissionFilter::getUserId, userId))
                .stream().map(SysUserPermissionFilter::getPermissioinCode)
                .collect(Collectors.toList());
    }

    @Override
    public List<String> getUserFilterPermsByName(String username) {
        //1、校验用户存在
        SysUser user = sysUserService.getUserByName(username);
        if (user == null) throw new RuntimeException("用户不存在！");
        return list(new LambdaQueryWrapper<SysUserPermissionFilter>()
                .eq(SysUserPermissionFilter::getTenantId, identityService.getTenantId())
                .eq(SysUserPermissionFilter::getUsername, username))
                .stream().map(SysUserPermissionFilter::getPermissioinCode)
                .collect(Collectors.toList());
    }

    @Override
    public List<String> getUserFilterPermissionIdsByName(String username) {
        //1、校验用户存在
        SysUser user = sysUserService.getUserByName(username);
        if (user == null) throw new RuntimeException("用户不存在！");
        return list(new LambdaQueryWrapper<SysUserPermissionFilter>()
                .eq(SysUserPermissionFilter::getTenantId, identityService.getTenantId())
                .eq(SysUserPermissionFilter::getUsername, username))
                .stream().map(SysUserPermissionFilter::getPermissionId)
                .collect(Collectors.toList());
    }

}
